
/**
 * 添加好友
 * @param {*} characters 
 * @param {*} target_id 
 * @returns 
 */
export function add_friend(characters, target_id) {
    let characters_id = characters["id"];
    if (!_CharactersData.has(target_id)) {
        ClientUtils.send_characters_basic_prompt(characters_id, "玩家不存在或已下线");
        return;
    }
    if (target_id === characters["id"]) {
        ClientUtils.send_characters_basic_prompt(characters_id, "#Y你无法添加自己为好友");
        return;
    }
    if (characters["好友数据"]["好友"].length >= 50) {
        ClientUtils.send_characters_basic_prompt(characters_id, "#Y你的好友人数已达上限");
        return;
    }
    for (let index = 0; index < characters["好友数据"]["好友"].length; index++) {
        let element = characters["好友数据"]["好友"][index];
        if (element["id"] === target_id) {
            ClientUtils.send_characters_basic_prompt(characters_id, "#Y对方已经是你的好友");
            return;
        }
    }
    let target_characters = _CharactersData.get(target_id);
    characters["好友数据"]["好友"].push({
        "id": target_characters["id"],
        "名称": target_characters["名称"],
        "等级": target_characters["等级"],
        "模型": target_characters["模型"],
        "门派": target_characters["门派"],
        "帮派": target_characters["BPMC"],
        "称谓": target_characters["当前称谓"],
        "关系": "陌生人",
        "好友度": 0,
    });
    // 从临时好友删除
    for (let index = 0; index < characters["好友数据"]["临时"].length; index++) {
        let element = characters["好友数据"]["临时"][index];
        if (element["id"] === target_id) {
            characters["好友数据"]["临时"].splice(index, 1);
        }
    }
    // 从黑名单移除
    for (let index = 0; index < characters["好友数据"]["黑名单"].length; index++) {
        let element = characters["好友数据"]["黑名单"][index];
        if (element["id"] === target_id) {
            characters["好友数据"]["黑名单"].splice(index, 1);
            break;
        }
    }
    // 如果也在对方好友列表则更新关系
    for (let index = 0; index < target_characters["好友数据"]["好友"].length; index++) {
        let element = target_characters["好友数据"]["好友"][index];
        if (element["id"] === characters["id"]) {
            element["关系"] = "普通朋友";
            characters["好友数据"]["好友"][characters["好友数据"]["好友"].length - 1]["关系"] = "普通朋友";
        }
    }
    // 通知客户端
    ClientUtils.send_characters_basic_prompt(characters_id, "#Y添加好友成功");
    ClientUtils.send_characters_basic_prompt(target_id, `#Y${characters["名称"]}将你添加为好友`);
    ClientUtils.send_characters_client_message(characters_id, 52);
    ClientUtils.send_characters_client_message(target_id, 52);
}

/**
 * 添加临时好友
 * @param {*} characters 
 * @param {*} target_id 
 * @returns 
 */
export function add_tmp_friend(characters, target_id) {
    let characters_id = characters["id"];
    if (!_CharactersData.has(target_id)) {
        ClientUtils.send_characters_basic_prompt(characters_id, "玩家不存在或已下线");
        return;
    }
    if (target_id === characters["id"]) {
        ClientUtils.send_characters_basic_prompt(characters_id, "#Y你无法添加自己为好友");
        return;
    }
    for (let index = 0; index < characters["好友数据"]["好友"].length; index++) {
        let element = characters["好友数据"]["好友"][index];
        if (element["id"] === target_id) {
            ClientUtils.send_characters_basic_prompt(characters_id, "#Y对方已经是你的好友");
            return;
        }
    }
    for (let index = 0; index < characters["好友数据"]["临时"].length; index++) {
        let element = characters["好友数据"]["临时"][index];
        if (element["id"] === target_id) {
            ClientUtils.send_characters_basic_prompt(characters_id, "#Y对方已经是你的临时好友");
            return;
        }
    }
    if (characters["好友数据"]["临时"].length >= 50) {
        ClientUtils.send_characters_basic_prompt(characters_id, "#Y你的临时好友人数已达上限");
        return;
    }
    // 从黑名单移除
    for (let index = 0; index < characters["好友数据"]["黑名单"].length; index++) {
        let element = characters["好友数据"]["黑名单"][index];
        if (element["id"] === target_id) {
            characters["好友数据"]["黑名单"].splice(index, 1);
            break;
        }
    }
    let target_characters = _CharactersData.get(target_id);
    characters["好友数据"]["临时"].push({
        "id": target_characters["id"],
        "名称": target_characters["名称"],
        "等级": target_characters["等级"],
        "模型": target_characters["模型"],
        "门派": target_characters["门派"],
        "帮派": target_characters["BPMC"],
        "称谓": target_characters["当前称谓"],
        "关系": "陌生人",
        "好友度": 0,
    });
    // 通知客户端
    ClientUtils.send_characters_basic_prompt(characters_id, "#Y添加临时好友成功");
    ClientUtils.send_characters_basic_prompt(target_id, `#Y${characters["名称"]}将你添加为临时好友`);
    ClientUtils.send_characters_client_message(characters_id, 52);
    ClientUtils.send_characters_client_message(target_id, 52);
}


export function remove_friend(characters, target_id, type) {
    for (let index = 0; index < characters["好友数据"][type].length; index++) {
        let element = characters["好友数据"][type][index];
        if (element["id"] === target_id) {
            characters["好友数据"][type].splice(index, 1);
            ClientUtils.send_characters_basic_prompt(characters["id"], "#G操作成功");
            ClientUtils.send_characters_client_message(characters["id"], 52);
            break;
        }
    }
}


export function add_black_list(characters, target_id) {
    let characters_id = characters["id"];
    if (!_CharactersData.has(target_id)) {
        ClientUtils.send_characters_basic_prompt(characters_id, "玩家不存在或已下线");
        return;
    }
    if (target_id === characters["id"]) {
        ClientUtils.send_characters_basic_prompt(characters_id, "#Y你无法添加自己到黑名单");
        return;
    }
    for (let index = 0; index < characters["好友数据"]["黑名单"].length; index++) {
        let element = characters["好友数据"]["黑名单"][index];
        if (element["id"] === target_id) {
            ClientUtils.send_characters_basic_prompt(characters["id"], "#Y对方已在黑名单中");
            return;
        }
    }
    if (characters["好友数据"]["黑名单"].length >= 50) {
        ClientUtils.send_characters_basic_prompt(characters_id, "#Y你的黑名单人数已达上限");
        return;
    }
    // 从好友列表移除
    for (let index = 0; index < characters["好友数据"]["好友"].length; index++) {
        let element = characters["好友数据"]["好友"][index];
        if (element["id"] === target_id) {
            characters["好友数据"]["好友"].splice(index, 1);
            break;
        }
    }
    // 从临时好友列表移除
    for (let index = 0; index < characters["好友数据"]["临时"].length; index++) {
        let element = characters["好友数据"]["临时"][index];
        if (element["id"] === target_id) {
            characters["好友数据"]["临时"].splice(index, 1);
            break;
        }
    }
    let target_characters = _CharactersData.get(target_id);
    characters["好友数据"]["黑名单"].push({
        "id": target_characters["id"],
        "名称": target_characters["名称"],
        "等级": target_characters["等级"],
        "模型": target_characters["模型"],
        "门派": target_characters["门派"],
        "帮派": target_characters["BPMC"],
        "称谓": target_characters["当前称谓"],
        "关系": "陌生人",
        "好友度": 0,
    });
    ClientUtils.send_characters_basic_prompt(characters["id"], "#G操作成功");
    ClientUtils.send_characters_client_message(characters["id"], 52);
}
